﻿using StandXunYouWeb.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace StandXunYouWeb
{
    /// <summary>
    /// 自定义验证逻辑
    /// </summary>
    public class AuthorManage
    {
        public class TokenModel
        {
            /// <summary>
            /// 颁发给谁
            /// </summary>
            public string UserName { get; set; }

            /// <summary>
            /// 随机字符
            /// </summary>
            public string Token { get; set; }

            /// <summary>
            /// 到期时间  默认30分钟
            /// </summary>
            public int Expire { get; set; } = 30;

            /// <summary>
            /// token创建时间
            /// </summary>
            public DateTime CreateTime { get; set; }

            /// <summary>
            /// 用户Id
            /// </summary>
            public string? UserIds { get; set; }

        }

        /// <summary>
        /// Token 验证地址
        /// </summary>
        private static Dictionary<string, TokenModel> TokenList = new Dictionary<string, TokenModel>();


        public static void RemoveToken(string EnCodeToken)
        {
            try
            {
                //解密token
                var token = AESEncryptHelper.Decode(EnCodeToken, AESEncryptHelper.AppKey);

                //反序列化
                var model = JsonConvert.DeserializeObject<TokenModel>(token);
                if (model != null)
                {
                    if (TokenList.ContainsKey(model.UserName))
                    {
                        TokenList.Remove(model.UserName);
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 获取加密的token
        /// </summary>
        /// <param name="UserName">登录成功后的用户名</param>
        /// <returns></returns>
        public static string GetToken(string UserName, string UserIds)
        {
            //处理原token
            if (TokenList.ContainsKey(UserName))
            {
                TokenList.Remove(UserName);
            }


            //token 内容
            var model = new TokenModel
            {
                CreateTime = DateTime.Now,
                UserName = UserName,
                UserIds = UserIds,
                Token = CommonHelpers.GenerateRandom(18)  //保存未加密的token
            };

            //aes加密
            var DeCodeToken = AESEncryptHelper.Encode(JsonConvert.SerializeObject(model), AESEncryptHelper.AppKey);
            TokenList.Add(UserName, model);

            return DeCodeToken;  //返回已加密的token
        }

        /// <summary>
        /// 验证token
        /// </summary>
        /// <param name="EnCodeToken">加密的token</param>
        /// <returns></returns>
        public static bool VerifyToken(string EnCodeToken)
        {
            //解密token
            var token = AESEncryptHelper.Decode(EnCodeToken, AESEncryptHelper.AppKey);

            try
            {
                //反序列化
                var model = JsonConvert.DeserializeObject<TokenModel>(token);
                if (model != null)
                {
                    //验证token
                    TokenModel newModel = null;
                    if (TokenList.TryGetValue(model.UserName, out newModel))
                    {
                        DateTime dt = model.CreateTime.AddMinutes(model.Expire);

                        //验证生效时间
                        if (model.Token.Equals(newModel.Token) && DateTime.Now < dt)
                        {
                            return true;
                        }
                        //到期移除
                        TokenList.Remove(newModel.UserName);
                    }
                }
            }
            catch (Exception ex)
            {
            }


            return false;
        }



        #region 根据token获取信息

        /// <summary>
        /// 根据token 获取用户名
        /// </summary>
        /// <param name="EnCodeToken"></param>
        /// <returns></returns>
        public static string GetUserNameFourToken(string EnCodeToken)
        {
            //解密token
            var token = AESEncryptHelper.Decode(EnCodeToken, AESEncryptHelper.AppKey);

            try
            {
                //反序列化
                var model = JsonConvert.DeserializeObject<TokenModel>(token);
                if (model != null)
                {
                    return model.UserName;
                }
            }
            catch
            {
            }


            return string.Empty;
        }

        /// <summary>
        /// 根据token 获取用户id
        /// </summary>
        /// <param name="EnCodeToken"></param>
        /// <returns></returns>
        public static string GetUserIdFourToken(string EnCodeToken)
        {
            //解密token
            var token = AESEncryptHelper.Decode(EnCodeToken, AESEncryptHelper.AppKey);

            //反序列化
            var model = JsonConvert.DeserializeObject<TokenModel>(token);
            if (model != null)
            {
                return model.UserIds;
            }


            return string.Empty;
        }



        public static TokenModel GeTokenModel(string EnCodeToken)
        {
            //解密token
            var token = AESEncryptHelper.Decode(EnCodeToken, AESEncryptHelper.AppKey);

            //反序列化
            var model = JsonConvert.DeserializeObject<TokenModel>(token);
            if (model != null)
            {
                return model;
            }


            return null;
        }
        #endregion
    }
}
